package com.orgcent.activities;

import java.util.Arrays;
import java.util.Date;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.orgcent.util.Tools;
/**
 * 短信阅读器
 */
public class SMSReader extends ListActivity {

	private static final String TAG = SMSReader.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sms_list);
		
		View view = getLayoutInflater().inflate(R.layout.sms_list_header, null);
		getListView().addHeaderView(view);
		view = getLayoutInflater().inflate(R.layout.sms_list_footer, null);
		getListView().addFooterView(view);
		
		Cursor c = managedQuery(Uri.parse("content://sms/"), null, null, null, "_id desc");
		
		Log.i(TAG, "column Name : " + Arrays.toString(c.getColumnNames()));
		StringBuilder content = new StringBuilder();
		while(c.moveToNext()) {
			content.delete(0, content.length());
			for(int i = 0; i < c.getColumnCount(); i++) {
				content.append(c.getColumnName(i)).append("=")
					   .append(c.getString(i)).append(",");
			}
			content.append("body=").append(c.getString(c.getColumnIndex("body")));
			Log.i(TAG, content.toString());
		}
		
//		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.sms_item_from, c, 
//				new String[]{"date","body"}, new int[]{R.id.sms_time_tv,R.id.sms_content_itv});
		SMSAdapter adapter = new SMSAdapter(this, c);
		
		setListAdapter(adapter);
	}

	private class SMSAdapter extends CursorAdapter {

		public SMSAdapter(Context context, Cursor c) {
			super(context, c);
		}

		public SMSAdapter(Context context, Cursor c, boolean autoRequery) {
			super(context, c, autoRequery);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			
			Log.i(TAG, "adapter -- new view");
			
			View itemView = LayoutInflater.from(context).inflate(R.layout.sms_list_item, parent,false);
			
			ViewHolder holder = new ViewHolder();
			holder.who_tv = (TextView) itemView.findViewById(R.id.sms_who_tv);
			holder.content_tv = (TextView) itemView.findViewById(R.id.sms_content_itv);
			holder.time_tv = (TextView) itemView.findViewById(R.id.sms_time_tv);
			itemView.setTag(holder);
			
			return itemView;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			Log.i(TAG, "adapter -- bind view");
			final ViewHolder holder = (ViewHolder) view.getTag();
			holder.who_tv.setText("来自:");
			holder.who_tv.append(cursor.getString(cursor.getColumnIndexOrThrow("address")));
			holder.content_tv.setText(cursor.getString(cursor.getColumnIndexOrThrow("body")));
			holder.time_tv.setText(Tools.date2str(new Date(cursor.getLong(cursor.getColumnIndexOrThrow("date")))));
			//根据类型，设置背景
			int type = cursor.getInt(cursor.getColumnIndexOrThrow("type"));
			final LinearLayout.LayoutParams params = (LayoutParams) holder.content_tv.getLayoutParams();
			if(2 == type) {
				holder.content_tv.setBackgroundResource(R.drawable.chatfrom_bg);
				params.gravity = Gravity.LEFT;
			} else {
				holder.content_tv.setBackgroundResource(R.drawable.chatto_bg);
				params.gravity = Gravity.RIGHT;
			} 
		}
		
		class ViewHolder {
			TextView who_tv;
			TextView time_tv;
			TextView content_tv;
		}
	}
}
